Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Generalize Buffer.BlockCopy optimization for either src or dst - #4736

Merged
jkotas merged 2 commits into
dotnet:masterfrom
jamesqo:generalize-bytearray-opt
May 3, 2016
Merged

jkotas merged 2 commits into
dotnet:masterfrom
jamesqo:generalize-bytearray-opt

Conversation

@jamesqo

@jamesqo jamesqo commented May 3, 2016

Copy link
Copy Markdown

Don't have that much experience in native programming, but here it goes...

I noticed from #3118 that an optimization was added in the case where both src and dst were byte arrays, but not when only one of them was. Therefore, the optimization would not apply to code like this:

var bytes = new byte[4] { 1, 2, 3, 4 };
var shorts = new shorts[2];
Buffer.BlockCopy(bytes, 0, shorts, 0, bytes.Length);

This PR changes that so the optimization runs if either the src or dst arrays is a byte array, otherwise falling back to the default implementation as usual.

cc @jkotas

@jkotas

jkotas commented May 3, 2016 •

Copy link
Copy Markdown
Member

I have never seen Buffer.BlockCopy usage pattern like this. Do you have any examples of code where this helps?

Checking for a special case - that pretty much never happens - is making the code slower for everybody else. It is net loss - not making any real world workload faster.

@jamesqo

jamesqo commented May 3, 2016

Copy link
Copy Markdown
Author

@jkotas Sure, it's one of the top answers on Buffer.BlockCopy on StackOverflow: http://stackoverflow.com/a/3783189/4077294

Besides (correct me if I'm wrong) I don't think this is adding any overhead to the cases where this optimization doesn't apply.

Comment thread src/vm/comutilnative.cpp Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: fro -> from

@jkotas

jkotas commented May 3, 2016

Copy link
Copy Markdown
Member

I see. LGTM. Thanks!

@jkotas
jkotas merged commit 32e44ef into dotnet:master May 3, 2016
@jamesqo
jamesqo deleted the generalize-bytearray-opt branch May 3, 2016 11:51
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…t/coreclr#4736)

* Generalize Buffer.BlockCopy optimization for either src or dst

Commit migrated from dotnet/coreclr@32e44ef
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants